<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class Authenticate
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @param  string|null $guard
     * @return mixed
     */
    public function handle ($request, Closure $next, $guard = null) {
        if (Auth::guard($guard)->guest()) {

            $logAddress = env('SSO_HOST') . 'sso-login.do?appId=' . env('SSO_APPID');
            if ($request->ajax() || $request->wantsJson()) {
                return response([
                    'message' => '需要登陆',
                    'status_code' => 599,
                    'data' => $logAddress
                ],599);
            }
            else {
                return redirect()->guest($logAddress);
            }

        }
        return $next($request);
    }
}
